clear all
set matsize 1000

use "$LocalData\BartikAnalyticData_2digit.dta", clear

drop if state==.
keep if year>=2003 & year<=2017 & abs(gsmall_dep1)<5
		gen miss = missing(gsmall_dep1, gSmallFirm1)
		keep if miss==0


gen weight=1
local weight weight

local y gsmall_dep1
local x gSmallFirm1

local ind_stub c_i_share_2000
local growth_stub g_nat_ind_size1_1_

local time_var year
local cluster_var state

 qui tab year, gen(year_)
 drop year_1

levelsof `time_var', local(years)

/** Demean growth rates **/
egen mean_growth = rowmean(`growth_stub'*)
foreach growth of varlist `growth_stub'* {
	qui replace `growth' = `growth' - mean_growth
	}
drop mean_growth




foreach year in `years' {
	foreach ind_var of varlist `ind_stub'* {
		gen t`year'_`ind_var' = `ind_var' * (year == `year')
		}
	foreach var of varlist `growth_stub'* {
		gen t`year'_`var'b = `var' if year == `year'
		egen t`year'_`var' = max(t`year'_`var'b), by(geography)
		drop t`year'_`var'b
		replace t`year'_`var' = 0 if t`year'_`var' == .
		}
*	foreach ind_var of varlist `controls' {
*		if `year' != 2000 {
*			gen t`year'_`ind_var' = `ind_var' * (year == `year')
*			}
*		}
	}

qui desc t*_`growth_stub'*, varlist full
disp wordcount(r(varlist))
qui desc t*_`ind_stub'*, varlist
disp wordcount(r(varlist))

egen test = rowtotal(`ind_stub'*), 
foreach ind_var of varlist `ind_stub'* {
	replace `ind_var' = `ind_var' / test
	if regexm("`ind_var'", "`ind_stub'(.*)") {
		local ind_num = regexs(1)
		replace `growth_stub'`ind_num' = 0 if `growth_stub'`ind_num' == .
		gen b_`ind_num' = `ind_var' * `growth_stub'`ind_num'
		}
	}
egen z3 = rowtotal(b_*)
drop b_*


local controls year_*



foreach var of varlist `ind_stub'* {
	if regexm("`var'", "`ind_stub'(.*)") {
		local ind = regexs(1) 
		}
	tempvar temp
	qui gen `temp' = `var' * `growth_stub'`ind'
	qui regress `x' `temp' `controls' [aweight=`weight'], cluster(geography) absorb(geography) 
	local pi_`ind' = _b[`temp']
	qui test `temp'
	local F_`ind' = r(F)
	qui reghdfe  `y' `temp' `controls'   [aweight=`weight'], cluster(geography) absorb(geography)
	local gamma_`ind' = _b[`temp']
	qui ivreghdfe  `y' `controls' (`x'=`temp') [aweight=`weight'], cluster(geography)absorb(geography)
	local beta_`ind' = string(_b[`x'], "%9.3f")
	drop `temp'
	}

	
 foreach var of varlist `ind_stub'21 `ind_stub'31 `ind_stub'23 `ind_stub'62 `ind_stub'11 {
 	if regexm("`var'", "`ind_stub'(.*)") {
 		local ind = regexs(1) 
 		}
 	tempvar temp
 	qui gen `temp' = `var' * `growth_stub'`ind'
 	ch_weak, p(.05) beta_range(-10(.1)10)   y(`y') x(`x') z(`temp') controls(`controls') weight(`weight')  cluster(geography) absorb(geography)
 	disp r(beta_min) ,  r(beta_max)
 	local ci_min_`ind' =string( r(beta_min), "%9.2f")
 	local ci_max_`ind' = string( r(beta_max), "%9.2f")
 	disp "`ind', `beta_`ind'', `F_`ind'', [`ci_min_`ind'', `ci_max_`ind'']"
 	drop `temp'
 	}


preserve
keep `ind_stub'* state geography year `weight'
reshape long `ind_stub', i(geography year) j(ind)
gen `ind_stub'pop = `ind_stub'*`weight'
collapse (sd) `ind_stub'sd = `ind_stub' (rawsum) `ind_stub'pop `weight' [aweight = `weight'], by(ind year)
tempfile tmp
save `tmp'
restore


bartik_weight, z(t*_`ind_stub'*)    weightstub(t*_`growth_stub'*) x(`x') y(`y') controls(`controls') weight_var(`weight') absorb(geography)


mat beta = r(beta)
mat alpha = r(alpha)
mat gamma = r(gam)
mat pi = r(pi)
mat G = r(G)
qui desc t*_`ind_stub'*, varlist
local varlist = r(varlist)


clear
svmat beta
svmat alpha
svmat gamma
svmat pi
svmat G


gen ind = ""
gen year = ""
local t = 1
foreach var in `varlist' {
	if regexm("`var'", "t(.*)_`ind_stub'(.*)") {
		qui replace year = regexs(1) if _n == `t'
		qui replace ind = regexs(2) if _n == `t'
		}
	local t = `t' + 1
	}

/** Calculate Panel C: Variation across years in alpha **/
	total alpha1 if year == "2003"
	mat b = e(b)
	local sum_2003_alpha = string(b[1,1], "%9.3f")
	total alpha1 if year == "2004"
	mat b = e(b)
	local sum_2004_alpha = string(b[1,1], "%9.3f")
	total alpha1 if year == "2005"
	mat b = e(b)
	local sum_2005_alpha = string(b[1,1], "%9.3f")
	total alpha1 if year == "2006"
	mat b = e(b)
	local sum_2006_alpha = string(b[1,1], "%9.3f")
	total alpha1 if year == "2007"
	mat b = e(b)
	local sum_2007_alpha = string(b[1,1], "%9.3f")
	total alpha1 if year == "2008"
	mat b = e(b)
	local sum_2008_alpha = string(b[1,1], "%9.3f")
	total alpha1 if year == "2009"
	mat b = e(b)
	local sum_2009_alpha = string(b[1,1], "%9.3f")
	total alpha1 if year == "2010"
	mat b = e(b)
	local sum_2010_alpha = string(b[1,1], "%9.3f")
	total alpha1 if year == "2011"
	mat b = e(b)
	local sum_2011_alpha = string(b[1,1], "%9.3f")
	total alpha1 if year == "2012"
	mat b = e(b)
	local sum_2012_alpha = string(b[1,1], "%9.3f")
	total alpha1 if year == "2013"
	mat b = e(b)
	local sum_2013_alpha = string(b[1,1], "%9.3f")
	total alpha1 if year == "2014"
	mat b = e(b)
	local sum_2014_alpha = string(b[1,1], "%9.3f")
	total alpha1 if year == "2015"
	mat b = e(b)
	local sum_2015_alpha = string(b[1,1], "%9.3f")
	total alpha1 if year == "2016"
	mat b = e(b)
	local sum_2016_alpha = string(b[1,1], "%9.3f")
	mat b = e(b)
	total alpha1 if year == "2017"
	mat b = e(b)
	local sum_2017_alpha = string(b[1,1], "%9.3f")
	
	sum alpha1 if year == "2003"
	local mean_2003_alpha = string(r(mean), "%9.3f")
	sum alpha1 if year == "2004"
	local mean_2004_alpha = string(r(mean), "%9.3f")
	sum alpha1 if year == "2005"
	local mean_2005_alpha = string(r(mean), "%9.3f")
	sum alpha1 if year == "2006"
	local mean_2006_alpha = string(r(mean), "%9.3f")
	sum alpha1 if year == "2007"
	local mean_2007_alpha = string(r(mean), "%9.3f")
	sum alpha1 if year == "2008"
	local mean_2008_alpha = string(r(mean), "%9.3f")
	sum alpha1 if year == "2009"
	local mean_2009_alpha = string(r(mean), "%9.3f")
	sum alpha1 if year == "2010"
	local mean_2010_alpha = string(r(mean), "%9.3f")
	sum alpha1 if year == "2011"
	local mean_2011_alpha = string(r(mean), "%9.3f")
	sum alpha1 if year == "2012"
	local mean_2012_alpha = string(r(mean), "%9.3f")
	sum alpha1 if year == "2013"
	local mean_2013_alpha = string(r(mean), "%9.3f")
	sum alpha1 if year == "2014"
	local mean_2014_alpha = string(r(mean), "%9.3f")
	sum alpha1 if year == "2015"
	local mean_2015_alpha = string(r(mean), "%9.3f")
	sum alpha1 if year == "2016"
	local mean_2016_alpha = string(r(mean), "%9.3f")
	sum alpha1 if year == "2017"
	local mean_2017_alpha = string(r(mean), "%9.3f")

destring ind, replace
destring year, replace
merge 1:1 ind year using `tmp'
gen beta2 = alpha1 * beta1
gen indshare2 = alpha1 * (`ind_stub'pop)/`weight'
gen indshare_sd2 = alpha1 * `ind_stub'sd
gen G2 = alpha1 * G1

collapse (sum) alpha1 beta2 indshare2 indshare_sd2 G2 (mean) G1 , by(ind)
gen agg_beta = beta2 / alpha1
gen agg_indshare = indshare2 / alpha1
gen agg_indshare_sd = indshare_sd2 / alpha1
gen agg_g = G2/alpha1

gen industry=ind
drop ind


destring industry, replace
merge 1:1 industry using "$SourceData\IndustryMatch.dta"	
rename IndLabel ind_name
drop ind
rename industry ind
keep if _merge == 3

gsort -alpha1
list if _n<=5
/***
ind	alpha1	beta2	beta1	agg_beta
21
31
23
62
11
***/



capture file close fh
file open fh  using "$LocalData\Tables\rotemberg_summary_2digit.tex", write replace
file write fh "\toprule" _n

/** Panel A: Negative and Positive Weights **/
total alpha1 if alpha1 > 0
mat b = e(b)
local sum_pos_alpha = string(b[1,1], "%9.3f")
total alpha1 if alpha1 < 0
mat b = e(b)
local sum_neg_alpha = string(b[1,1], "%9.3f")

sum alpha1 if alpha1 > 0
local mean_pos_alpha = string(r(mean), "%9.3f")
sum alpha1 if alpha1 < 0
local mean_neg_alpha = string(r(mean), "%9.3f")

local share_pos_alpha = string(abs(`sum_pos_alpha')/(abs(`sum_pos_alpha') + abs(`sum_neg_alpha')), "%9.3f")
local share_neg_alpha = string(abs(`sum_neg_alpha')/(abs(`sum_pos_alpha') + abs(`sum_neg_alpha')), "%9.3f")



/** Panel B: Correlations of Industry Aggregates **/
gen F = .
gen agg_pi = .
gen agg_gamma = .
levelsof ind, local(industries)
display `industries'
foreach ind in `industries' {
	capture replace F = `F_`ind'' if ind == `ind'
	capture replace agg_pi = `pi_`ind'' if ind == `ind'
	capture replace agg_gamma = `gamma_`ind'' if ind == `ind'		
	}
corr alpha1 agg_g agg_beta F agg_indshare_sd
mat corr = r(C)
forvalues i =1/5 {
	forvalues j = `i'/5 {
		local c_`i'_`j' = string(corr[`i',`j'], "%9.3f")
		}
	}

/** Panel  D: Top 5 Rotemberg Weight Inudstries **/
foreach ind in 21 31 23 62 11 {
	qui sum alpha1 if ind == `ind'
   local alpha_`ind' = string(r(mean), "%9.3f")
	qui sum agg_g if ind == `ind'	
	local g_`ind' = string(r(mean), "%9.3f")
	/* qui sum agg_beta if ind == `ind'	 */
	/* local beta_`ind' = string(r(mean), "%9.3f") */
	qui sum agg_indshare if ind == `ind'	
	local share_`ind' = string(r(mean)*100, "%9.3f")
	tempvar temp
	qui gen `temp' = ind == `ind'
	gsort -`temp'
	local ind_name_`ind' = ind_name[1]
	drop `temp'
	}


/**** Make Overid Graph ***/
gen omega = alpha1*agg_beta
total omega
mat b = e(b)
local b = b[1,1]

gen label_var = ind 
gen beta_lab = string(agg_beta, "%9.3f")


gen abs_alpha = abs(alpha1) 
gen positive_weight = alpha1 > 0
gen agg_beta_pos = agg_beta if positive_weight == 1
gen agg_beta_neg = agg_beta if positive_weight == 0
twoway (scatter agg_beta_pos agg_beta_neg F if F >= 5 [aweight=abs_alpha ], msymbol(Oh Dh) ), legend(label(1 "Positive Weights") label( 2 "Negative Weights")) yline(`b', lcolor(black) lpattern(dash)) xtitle("First stage F-statistic")  ytitle("{&beta}{subscript:k} estimate")
graph export "$LocalData\Results\overid_2digit.pdf", replace

gsort -alpha1
twoway (scatter F alpha1 if _n <= 5, mcolor(dblue) mlabel(ind_name  ) msize(0.5) mlabsize(2) ) (scatter F alpha1 if _n > 5, mcolor(dblue) msize(0.5) ), name(a, replace) xtitle("Rotemberg weight") ytitle("First stage F-statistic") yline(10, lcolor(black) lpattern(dash)) legend(off)
graph export "$LocalData\Results\F_vs_rotemberg_weight_2digit.pdf", replace

/** Panel E: Weighted Betas by alpha weights **/

gen agg_beta_weight = agg_beta * alpha1
collapse (sum) agg_beta_weight alpha1 (mean)  agg_beta, by(positive_weight)
egen total_agg_beta = total(agg_beta_weight)
gen share = agg_beta_weight / total_agg_beta
gsort -positive_weight
local agg_beta_pos = string(agg_beta_weight[1], "%9.3f")
local agg_beta_neg = string(agg_beta_weight[2], "%9.3f")
local agg_beta_pos2 = string(agg_beta[1], "%9.3f")
local agg_beta_neg2 = string(agg_beta[2], "%9.3f")
local agg_beta_pos_share = string(share[1], "%9.3f")
local agg_beta_neg_share = string(share[2], "%9.3f")

/*** Write final table **/
/** Panel A **/
file write fh "\multicolumn{3}{l}{\textbf{Panel A: Negative and positive weights}}\\" _n
file write fh  " & Sum & Mean & Share \\  \cmidrule(lr){2-4}" _n
file write fh  "Negative & `sum_neg_alpha' & `mean_neg_alpha' & `share_neg_alpha' \\" _n
file write fh  "Positive & `sum_pos_alpha' & `mean_pos_alpha' & `share_pos_alpha' \\" _n

/** Panel B **/
file write fh "\multicolumn{5}{l}{\textbf{Panel B: Correlations of Industry Aggregates} }\\" _n
file write fh  " &$\alpha_k$ & \$g_{k}$ & $\beta_k$ & \$F_{k}$ & Var(\$z_k$) \\" _n
file write fh  "\cmidrule(lr){2-6} " _n
file write fh " & \\" _n
file write fh " $\alpha_k$             & 1\\" _n
file write fh " \$g_{k}$                &   `c_1_2'  & 1\\" _n
file write fh " $\beta_{k}$             &   `c_1_3'  & `c_2_3'    &1\\" _n
file write fh " \$F_{k}$                &   `c_1_4'  & `c_2_4'    &  `c_3_4'  & 1\\" _n
file write fh " Var(\$z_{k}$)           &   `c_1_5'  & `c_2_5'    &  `c_3_5'  &  `c_4_5'   &1\\" _n

/** Panel C **/

file write fh "\multicolumn{5}{l}{\textbf{Panel C: Variation across years in $\alpha_{k}$}}\\" _n
file write fh  " & Sum & Mean \\  \cmidrule(lr){2-3}" _n
file write fh  "2003 & `sum_2003_alpha' & `mean_2003_alpha' \\" _n
file write fh  "2004 & `sum_2004_alpha' & `mean_2004_alpha' \\" _n
file write fh  "2005 & `sum_2005_alpha' & `mean_2005_alpha' \\" _n
file write fh  "2006 & `sum_2006_alpha' & `mean_2006_alpha' \\" _n
file write fh  "2007 & `sum_2007_alpha' & `mean_2007_alpha' \\" _n
file write fh  "2008 & `sum_2008_alpha' & `mean_2008_alpha' \\" _n
file write fh  "2009 & `sum_2009_alpha' & `mean_2009_alpha' \\" _n
file write fh  "2010 & `sum_2010_alpha' & `mean_2010_alpha' \\" _n
file write fh  "2011 & `sum_2011_alpha' & `mean_2011_alpha' \\" _n
file write fh  "2012 & `sum_2012_alpha' & `mean_2012_alpha' \\" _n
file write fh  "2013 & `sum_2013_alpha' & `mean_2013_alpha' \\" _n
file write fh  "2014 & `sum_2014_alpha' & `mean_2014_alpha' \\" _n
file write fh  "2015 & `sum_2015_alpha' & `mean_2015_alpha' \\" _n
file write fh  "2016 & `sum_2016_alpha' & `mean_2016_alpha' \\" _n
file write fh  "2017 & `sum_2017_alpha' & `mean_2017_alpha' \\" _n

/** Panel D **/
file write fh "\multicolumn{5}{l}{\textbf{Panel D: Top 5 Rotemberg weight industries} }\\" _n
file write fh  " & $\hat{\alpha}_{k}$ & \$g_{k}$ & $\hat{\beta}_{k}$ & 95 \% CI & Ind Share \\ \cmidrule(lr){2-6}" _n
foreach ind in 21 31 23 62 11 {
	if `ci_min_`ind'' != -10 & `ci_max_`ind'' != 10 {
		file write fh  "`ind_name_`ind'' & `alpha_`ind'' & `g_`ind'' & `beta_`ind'' & (`ci_min_`ind'',`ci_max_`ind'')  & `share_`ind'' \\ " _n
		}
	else  {
		file write fh  "`ind_name_`ind'' & `alpha_`ind'' & `g_`ind'' & `beta_`ind'' & \multicolumn{1}{c}{N/A}  & `share_`ind'' \\ " _n
		}
	}
file write fh "\multicolumn{5}{l}{\textbf{Panel E: Estimates of $\beta_{k}$ for positive and negative weights} }\\" _n
file write fh  " & $\alpha$-weighted Sum & Share of overall $\beta$ & Mean  \\ \cmidrule(lr){2-3}" _n
file write fh  " Negative & `agg_beta_neg' & `agg_beta_neg_share' &`agg_beta_neg2' \\" _n
file write fh  " Positive & `agg_beta_pos' & `agg_beta_pos_share' & `agg_beta_pos2' \\" _n
file write fh  "\bottomrule" _n
file close fh
